package com.cardbaobao.cardbabyclient.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.controller.Controller;
import com.cardbaobao.cardbabyclient.model.Banner;
import com.cardbaobao.cardbabyclient.model.result.BannerResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/5 15:37.
 */

public class DebitIntroActivity extends BaseActivity {
    public static final String DEBIT_TYPE_I="debitType";
    public static final int QUICK_DEBIT=0;//快捷收款
    public static final int QR_CODE_DEBIT=1;//二维码收款
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.iv_pic)
    ImageView ivPic;

    private int debitType;//默认为快捷收款
    private String name;
    private String idCardNo;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_debit_intro;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //图片比例1：1.2
        ViewGroup.LayoutParams params=ivPic.getLayoutParams();
        final int width= ScreenUtil.getScreenWidth()- DensityUtil.dp2px(35);
        params.width=width;
        params.height= (int) (width*1.2f);
        ivPic.setLayoutParams(params);
        Intent intent=getIntent();
        name=intent.getStringExtra(IdentifyVerifyActivity.NAME_S);
        idCardNo=intent.getStringExtra(IdentifyVerifyActivity.ID_CARD_NO_S);
        debitType=intent.getIntExtra(DEBIT_TYPE_I,QUICK_DEBIT);

        if(debitType==QUICK_DEBIT){
            mCurrentPageId=Self.Page.app_quick_receivables_introduction;
        }else{
            mCurrentPageId=Self.Page.app_qrcode_receivables_introduction;
        }

        titleBar.setRightViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //埋点
                if(debitType==QUICK_DEBIT){
                    SelfUtils.buriedClickData(Self.Action.app_click_quick_receivables_introduction_common_problem,
                            Self.Action.app_click_quick_receivables_introduction_common_problem_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            mPrePageId);
                }else if(debitType==QR_CODE_DEBIT){
                    SelfUtils.buriedClickData(Self.Action.app_click_qrcode_receivables_introduction_common_problem,
                            Self.Action.app_click_qrcode_receivables_introduction_common_problem_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            mPrePageId);
                }
                Controller controller=new Controller(mContext);
                controller.toCommonQuestion(DebitIntroActivity.this,mCurrentPageId);
            }
        });

        Observable<String> observable=null;
        if(debitType==QR_CODE_DEBIT){
            titleBar.setTitleText("二维码收款");
            GoogleAnalyticsUtil.setScreenName("二维码收款介绍");
            observable= Api.getBannerData(Constant.GLB0332);
        }else if(debitType==QUICK_DEBIT){
            //默认为快捷收款
            titleBar.setTitleText("快捷收款");
            GoogleAnalyticsUtil.setScreenName("快捷收款介绍");
            observable= Api.getBannerData(Constant.GLB0331);
        }
        showProgressDialog(false);
        if(observable!=null){
            observable.subscribe(new Subscriber<String>() {
                @Override
                public void onCompleted() {
                    dismissProgressDialog();
                }

                @Override
                public void onError(Throwable throwable) {
                    dismissProgressDialog();
                    ToastUtil.showToast(getString(R.string.string_request_data_fail));
                }

                @Override
                public void onNext(String s) {
                    BannerResult result= JsonUtil.json2Object(s,BannerResult.class);
                    if(result.getCode()==Constant.SUC_CODE){
                        Banner banner=result.getData();
                        if(banner!=null){
                            List<Banner.CarouselFigureListBean> list=banner.getCarousel_figure_list();
                            if(list!=null&&list.size()>0){
                                ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL+list.get(0).getImage(),
                                        ivPic);
                            }
                        }
                    }
                }
            });
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }


    @Override
    protected void onResume() {
        super.onResume();
        if(debitType==QR_CODE_DEBIT){
            //二维码收款
            GoogleAnalyticsUtil.setScreenName("二维码收款介绍");
        }else{
            //默认为快捷收款
            GoogleAnalyticsUtil.setScreenName("快捷收款介绍");
        }
    }

    @OnClick(R.id.bt_debit_now)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_debit_now:
                //去实名认证
                Intent intent=new Intent(mContext,IdentifyVerifyActivity.class);
                if(debitType==QUICK_DEBIT){
                    //快捷收款
                    SelfUtils.buriedClickData(
                            Self.Action.app_click_quick_receivables_introduction_Immediate_receivables,
                            Self.Action.app_click_quick_receivables_introduction_Immediate_receivables_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            mPrePageId);
                    intent.putExtra(BankCardVerifyActivity.PAGE_FROM_I,
                            BankCardVerifyActivity.ACTIVITY_QUICK_DEBIT);
                }else if(debitType==QR_CODE_DEBIT){
                    //二维码收款
                    SelfUtils.buriedClickData(
                            Self.Action.app_click_qrcode_receivables_introduction_Immediate_receivables,
                            Self.Action.app_click_qrcode_receivables_introduction_Immediate_receivables_des,
                            mEnterPageTime,
                            mCurrentPageId,
                            mPrePageId);
                    intent.putExtra(BankCardVerifyActivity.PAGE_FROM_I,
                            BankCardVerifyActivity.ACTIVITY_QR_CODE_DEBIT);
                }
                intent.putExtra(IdentifyVerifyActivity.NAME_S,name);
                intent.putExtra(IdentifyVerifyActivity.ID_CARD_NO_S,idCardNo);
                intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                startActivity(intent);
                finish();
                break;
        }
    }
}
